package com.rebate.web.servlet;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rebate.model.Advert;
import com.rebate.service.IBaseService;
import com.rebate.util.Page;
import com.rebate.util.PageUtils;

@Controller
public class AdvertServlet extends BaseServlet{
	
	private String view = "/advert/listadvert";
	private String createView = "/advert/createadvert";
	private String editView = "/advert/editadvert";
	@Autowired
	private IBaseService<Advert> advertService;
	
	/**
	 * 推荐列表页面
	 * @throws IOException 
	 */
	@RequestMapping(value = "/advert/index.htm")
	public String index(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("advert") Advert advert) throws IOException {
		return view;
	}
	
	/**
	 * 添加推荐信息页面初始化
	 * @throws IOException 
	 */
	@RequestMapping(value = "/advert/add.htm")
	public String addInit(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("advert") Advert advert) throws IOException {
		return createView;
	}
	
	/**
	 * 保存推荐
	 * @throws IOException 
	 */
	@RequestMapping(value = "/advert/save.htm")
	public void save(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("advert") Advert advert) throws IOException {
		Long id = advertService.create(advert);
		if(id != null) {
			response.getWriter().print(1);
		}else {
			response.getWriter().print(0);
		}
	}
	
	/**
	 * 编辑推荐页面初始化
	 * @throws IOException 
	 */
	@RequestMapping(value = "/advert/edit.htm")
	public String editInit(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("advert") Advert advert) throws IOException {
		advert = advertService.findById(advert.getId());
		model.put("advert", advert);
		return editView;
	}
	
	/**
	 * 更新推荐信息
	 * @throws IOException 
	 */
	@RequestMapping(value = "/advert/update.htm")
	public void update(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("advert") Advert advert) throws IOException {
		if(advertService.update(advert)) {
			response.getWriter().print(1);
		}else {
			response.getWriter().print(0);
		}
	}
	
	/**
	 * 查询推荐List
	 * @throws IOException 
	 */
	@RequestMapping(value = "/advert/list.htm")
	public void getList(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("advert") Advert advert) throws IOException {
		Page<Advert> page = PageUtils.getPage(request);
		page = advertService.queryForListPage(page, advert);
		print(request, response, page);
	}
	/**
	 * remove merchanat
	 * @throws IOException 
	 */
	@RequestMapping(value = "/advert/remove.htm")
	public void remove(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("advert") Advert advert) throws IOException {
		if(advertService.delete(advert.getId())) {
			response.getWriter().print(1);
		}else {
			response.getWriter().print(0);
		}
	}
}
